package com.xiaobai.frame.service.impl;

import com.xiaobai.common.utils.SnowFlow;
import com.xiaobai.common.utils.config.Tree.TreeHelper;
import com.xiaobai.common.utils.result.R;
import com.xiaobai.common.utils.result.Result;
import com.xiaobai.common.utils.result.ResultCode;
import com.xiaobai.common.utils.tool.AuthUtil;
import com.xiaobai.common.utils.tool.Func;
import com.xiaobai.data.manager.system.RoleManager;
import com.xiaobai.data.manager.system.RoleMenuManager;
import com.xiaobai.data.manager.system.UserRoleManager;
import com.xiaobai.frame.service.IRoleService;
import com.xiaobai.model.convertor.system.RoleConvertor;
import com.xiaobai.model.entity.system.Role;
import com.xiaobai.model.vo.system.role.RoleTreeVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import static com.xiaobai.common.utils.tool.CommonConstants.ROOT_ID;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-24
 */
@Service
@AllArgsConstructor
public class RoleServiceImpl implements IRoleService {

    private final RoleManager roleManager;
    private final UserRoleManager userRoleManager;
    private final RoleMenuManager roleMenuManager;

    @Override
    public Result addRole(Role role) {
        if (Func.isEmpty(role)) {
            return R.fail(ResultCode.PARAMETER_NULL, "传入参数为null");
        }
//        Role one = this.getOne(new LambdaQueryWrapper<Role>().eq(Role::getId, role.getParentId()), false);
        role.setCreateTime(LocalDateTime.now());
        role.setCreateUser(AuthUtil.getId());
        role.setId(new SnowFlow(1, 4).nextId());
        if (roleManager.save(role)) {
            return R.success(ResultCode.SUCCESS_CREATE_RESOURCE, "新增成功");
        }
        return R.fail(ResultCode.INTERNAL_SERVER_ERROR, "新增失败,请联系管理人员");
    }

    @Override
    public Result updateRole(Role role) {
        if (Func.isEmpty(role) || Func.isNull(role.getId())) {
            return R.fail(ResultCode.PARAMETER_NULL, "传入参数为null");
        }
        if (roleManager.updateById(role)) {
            return R.success(ResultCode.SUCCESS_NO_CONTENT, "修改成功");
        }
        return R.fail(ResultCode.INTERNAL_SERVER_ERROR, "修改失败,请联系管理人员");
    }

    @Override
    @Transactional
    public Result deleteRole(Long[] ids) {
        if (Func.isEmpty(ids)) {
            return R.fail(ResultCode.PARAMETER_NULL, "传入参数为null");
        }
        List<Long> list = Arrays.asList(ids);
        boolean userRoleDelete = userRoleManager.deleteRoleIds(list);
        boolean roleMenuDelete = roleMenuManager.deleteRoleIds(list);
        if (roleManager.removeBatchByIds(list) && roleMenuDelete && userRoleDelete) {
            return R.success(ResultCode.SUCCESS_CREATE_RESOURCE, "删除成功");
        }
        return R.fail(ResultCode.INTERNAL_SERVER_ERROR, "删除失败,数据当中未有该对应数据");
    }

    @Override
    public Result listTreeRole(Long parentId) {
        List<Role> roleList = roleManager.listTreeRole(parentId);
        List<RoleTreeVO> treeVOList = RoleConvertor.INSTANCE.toTreeVOList(roleList);
        TreeHelper<Long, RoleTreeVO> helper = new TreeHelper<>(treeVOList, ROOT_ID);
        return R.success(ResultCode.SUCCESS, "查询成功", helper.create());
    }

    @Override
    public Result getRole(Long id) {
        if (Func.isNull(id)) {
            return R.fail(ResultCode.PARAMETER_NULL, "传入数据未空");
        }
        Role dept = roleManager.getById(id);
        if (Func.isEmpty(dept)) {
            return R.fail(ResultCode.NOT_SEEK_RESOURCE, "数据当中未有该id数据");
        }
        return R.success(ResultCode.SUCCESS,"查询成功", dept);
    }
}
